import random 
g = int(input('Введите четырёхзначное число: ')) 
v = 0 
if g > 9999 or g < 1000: 
    print ('0') 
def prost(b): 
    if b <= 1: 
        return False 
    for _ in range(5): 
        a = random.randint(1, b-1) 
        if pow(a, b-1, b) != 1: 
            return False 
        return True  
 
 
t = g // 1000 
if prost(t) == True: 
    v = t 
 
 
j = g // 100 
if prost(j) == True and j > v: 
    v =j
 
 
n = g // 10 
if prost(n) == True and n > v: 
    v =n 
 
 
z = g
if prost(z) == True and z > v: 
    v =z
 
 
x = g // 100 % 10 
if prost(x) == True and x > v: 
    v = x
 
 
m = g // 10 % 100 
if prost(m ) == True and m > v: 
    v =m
 
 
z = g % 1000 
if prost(z) == True and z > v: 
    v = z
 
 
l = g // 10 %10 
if prost(l) == True and l > v: 
    v = l
 
 
q = g % 100 
if prost(q) == True and q > v: 
    v = q
 
 
h = g % 10 
if prost(h) == True and h > v: 
    v = h 
 
 
print(v)